package com.example.rbac.config;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibProxy implements MethodInterceptor {
    // 获取代理对象
    public <T> T getProxy(Class<T> clazz) {
        return (T) Enhancer.create(clazz, this);
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object result = methodProxy.invokeSuper(o, objects);
        after();
        return result;
    }

    private void before() {
        System.out.println("cglib proxy before ...");
    }

    private void after() {
        System.out.println("cglib proxy after ...");
    }

    public static void main(String[] args) {
        CglibProxy cglibProxy = new CglibProxy();
        Hello2 proxy = cglibProxy.getProxy(Hello2.class);
        proxy.test();
//        InterfaceProxy interfaceProxy = new InterfaceProxy(new Hello2());
//        Hello2 proxy1 = interfaceProxy.getProxy();
//        proxy1.test();
    }
}
